package com.github.yealove.configgen.service.impl;

import com.github.yealove.configgen.exception.Assert;
import com.github.yealove.configgen.mapper.ConfigMapper;
import com.github.yealove.configgen.mapper.GroupMapper;
import com.github.yealove.configgen.mapper.TemplateMapper;
import com.github.yealove.configgen.model.Config;
import com.github.yealove.configgen.model.ConfigGenConstant;
import com.github.yealove.configgen.model.Group;
import com.github.yealove.configgen.model.Template;
import com.github.yealove.configgen.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigMapper configMapper;

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private TemplateMapper templateMapper;

    @Override
    public List<Config> getByGid(int gid) {
        return configMapper.getByGid(gid);
    }

    @Override
    public void saveConfigs(int gid, List<Config> configs) {
        configMapper.deleteByGid(gid);
        for (Config config : configs) {
            config.setGid(gid);
            if (StringUtils.isBlank(config.getCKey()) || "null".equals(config.getCValue())) {
                config.setCValue("");
            }
            configMapper.addConfig(config);
        }
    }

    @Override
    public void parseTemplate2Configs(int gid, List<Integer> tids) {
        Group group = groupMapper.getGroupById(gid);
        Assert.assertNotNull(group, "配置组不存在");
        List<Config> configs = configMapper.getByGid(gid);
        List<String> keys = new ArrayList<>();

        for (Integer tid : tids) {
            Template template = templateMapper.getTemplateById(tid);
            if (template == null) {
                log.error("模板不存在： " + tid);
                continue;
            }

            String content = template.getContent();
            if (StringUtils.isBlank(content)) {
                return;
            }

            Pattern pattern = Pattern.compile(ConfigGenConstant.prefix + "[.A-Za-z0-9_-]+" + ConfigGenConstant.suffix);
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                String str = matcher.group();
                String key = str.substring(ConfigGenConstant.prefix.length(), str.length() - ConfigGenConstant.suffix.length());
                if (keys.contains(key) || configs.stream().anyMatch(config -> config.getCKey().equals(key))) {
                    keys.add(key);
                    continue;
                }
                keys.add(key);
                configs.add(Config.builder().cKey(key).cValue("").gid(gid).sort(configs.size()).build());
            }
        }


        saveConfigs(gid, configs);
    }
}
